package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.ElderlyDTO;
import com.api.shopapi.entity.dto.RechargeDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 批量充值
 * */

@RestController
@RequestMapping("RechargeGroup")
public class RechargeGroupController  extends BaseController {

    @Autowired
    @Qualifier("RechargeGroupServiceImplOne")
    private RechargeGroupService rechargeGroupService;

    @Autowired
    @Qualifier("RechargeServiceImplOne")
    private RechargeService rechargeService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("ElderlyServiceImplOne")
    private ElderlyService elderlyService;

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;

    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            int pageIndex=jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/RechargeGroup/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            Staff staff =staffService.getByStaffCode(staffCode);
            String  governmentCode =staff.getGovernmentCode();
            PageInfo<RechargeGroup> rechargeGroupPageInfo =rechargeGroupService.getListForPage(pageIndex,pageSize,governmentCode);
            return success(rechargeGroupPageInfo);
        } catch (Exception e) {
            insertError("/RechargeGroup/Page",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //批量充值详情
    @RequestMapping(value = "RechargeInfo",method = RequestMethod.POST)
    public MyResult RechargeInfo(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            int pageIndex=jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");
            String rechargeGroupCode =jsonObject.getString("rechargeGroupCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/RechargeGroup/RechargeInfo");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }




            PageInfo<RechargeDTO> rechargeDTOPageInfo=rechargeService.getForInfo(pageIndex,pageSize,rechargeGroupCode);
            List<RechargeDTO> rechargeDTOList=rechargeDTOPageInfo.getList();
            for (RechargeDTO rechargeDTO:rechargeDTOList){
                ElderlyDTO elderlyDTO =elderlyService.getByElderlyCode(rechargeDTO.getElderlyCode());
                rechargeDTO.setElderlyDTO(elderlyDTO);
                User user =userService.getByUserCode(elderlyDTO.getUserCode());
                rechargeDTO.setUserPhone(user.getPhone());
            }
            return success(rechargeDTOPageInfo);
        } catch (Exception e) {
            insertError("/RechargeGroup/RechargeInfo",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "PageToGovPlatform",method = RequestMethod.POST)
    public MyResult PageToGovPlatform(@RequestBody JSONObject jsonObject){
        try {
            int pageIndex=jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");
            String governmentCode = jsonObject.getString("governmentCode");
            PageInfo<RechargeGroup> rechargeGroupPageInfo =rechargeGroupService.getListForPage(pageIndex,pageSize,governmentCode);
            return success(rechargeGroupPageInfo);
        } catch (Exception e) {
            insertError("/RechargeGroup/PageToGovPlatform",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //批量充值详情
    @RequestMapping(value = "RechargeInfoToGovPlatform",method = RequestMethod.POST)
    public MyResult RechargeInfoToGovPlatform(@RequestBody JSONObject jsonObject){
        try {
            int pageIndex=jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");
            String rechargeGroupCode =jsonObject.getString("rechargeGroupCode");

            PageInfo<RechargeDTO> rechargeDTOPageInfo=rechargeService.getForInfo(pageIndex,pageSize,rechargeGroupCode);
            List<RechargeDTO> rechargeDTOList=rechargeDTOPageInfo.getList();
            for (RechargeDTO rechargeDTO:rechargeDTOList){
                ElderlyDTO elderlyDTO =elderlyService.getByElderlyCode(rechargeDTO.getElderlyCode());
                rechargeDTO.setElderlyDTO(elderlyDTO);
                User user =userService.getByUserCode(elderlyDTO.getUserCode());
                rechargeDTO.setUserPhone(user.getPhone());
            }
            return success(rechargeDTOPageInfo);
        } catch (Exception e) {
            insertError("/RechargeGroup/RechargeInfoToGovPlatform",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }



}
